﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using Rhino.Mocks;
using SimpleServiceBus.Bus;
using SimpleServiceBus.Bus.MessageManagement.MessageValueProviders;
using SimpleServiceBus.Bus.Pipeline;
using SimpleServiceBus.Endpoint;
using SimpleServiceBus.Endpoint.Metadata;
using SimpleServiceBus.Utilities.ExpressionEval;

namespace SimpleServiceBus.Tests.Bus.MessageManagement
{
    [TestFixture]
    public class MessageValueProvidersTestFixture
    {
        [Test]
        public void DynamicMessageValueProviderShouldExecuteLamdaWithMissingContext()
        {
            //given
            var provider = new DynamicMessageValueProvider<string>(s => s.Length.ToString());
            var message = new MessageEnvelope {Body = "hello"};

            //when
            var result = provider.GetMessageValue(message, null);

            //then
            Assert.That(result, Is.EqualTo("5"));
        }

        [Test]
        public void DynamicMessageValueProviderShouldExecuteLamdaWithProvidedContext()
        {
            //given
            var provider = new DynamicMessageValueProvider<string>((s, c) => s.Length.ToString() + ":" + c.ToString());
            var message = new MessageEnvelope {Body = "hello"};
            var ctx = new MessageContext(message, null);


            //when
            var result = provider.GetMessageValue(message, ctx);

            //then
            Assert.That(result, Is.EqualTo("5:SimpleServiceBus.Bus.Pipeline.MessageContext"));
        }

        [Test]
        public void ExpressionMessageValueProviderShouldEvalProvidedExpression()
        {
            //given
            var provider = new ExpressionMessageValueProvider("Length");
            var message = new MessageEnvelope {Body = "hello"};
            var ctx = MockRepository.GenerateStub<IMessageContext>();
            var eval = new MessageExpressionEvaluator();
            ctx.Stub(c => c.GetLocalService<IMessageExpressionEvaluator>()).Return(eval);

            //when
            var result = provider.GetMessageValue(message, ctx);

           //then
            Assert.That(result, Is.EqualTo("5"));
        }
        
    }
}